package mands.utils.weather;

import java.io.StringReader;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

import android.util.Log;

/**
 * WeatherParser.java
 * 
 * @author Maze
 * 
 *         Parsing methods that parse weather information from an xml String.
 */
public class WeatherParser {

	/**
	 * Parses the raw xml string and sets the current conditions.
	 * 
	 * @param currentConditions
	 *            The xml nodes of the current conditions.
	 */
	public static CurrentConditions parseCurrentConditions(String rawXmlWeather) {
		CurrentConditions condition = new CurrentConditions();
		try {
			DocumentBuilder builder = DocumentBuilderFactory.newInstance()
					.newDocumentBuilder();
			Document doc = builder.parse(new InputSource(new StringReader(
					rawXmlWeather)));

			NodeList currentConditions = doc
					.getElementsByTagName("current_conditions");

			Element element = (Element) currentConditions.item(0);

			condition.condition = parseTag(element, "condition");
			condition.temperature = Integer
					.parseInt(parseTag(element, "temp_f"));
			condition.humidity = parseTag(element, "humidity");
			condition.wind = parseTag(element, "wind_condition");

		} catch (Exception e) {
			Log.e("parseCurrentConditions", e.toString());
		}

		return condition;
	}

	/**
	 * Parses the raw xml string and sets the forecast conditions.
	 * 
	 * @param rawXmlWeather
	 *            The xml nodes of the forecast conditions.
	 * @return The weather forecast conditions.
	 */
	public static WeatherForecast[] parseWeatherForecasts(String rawXmlWeather) {
		WeatherForecast[] weatherForecasts = new WeatherForecast[4];
		try {
			DocumentBuilder builder = DocumentBuilderFactory.newInstance()
					.newDocumentBuilder();
			Document doc = builder.parse(new InputSource(new StringReader(
					rawXmlWeather)));

			for (int i = 0; i < 4; i++) {
				WeatherForecast wForecast = new WeatherForecast();

				weatherForecasts[i] = wForecast;
			}

			NodeList forecast = doc.getElementsByTagName("forecast_conditions");

			for (int j = 0; j < 4; j++) {
				Element element = (Element) forecast.item(j);

				weatherForecasts[j].day = WeatherForecast
						.dataToString(parseTag(element, "day_of_week"));
				weatherForecasts[j].lowTemperature = Integer.parseInt(parseTag(
						element, "low"));
				weatherForecasts[j].highTemperature = Integer
						.parseInt(parseTag(element, "high"));
				weatherForecasts[j].weatherCondition = parseTag(element,
						"condition");
			}

		} catch (Exception e) {
			Log.e("parseCurrentConditions", e.toString());
		}
		return weatherForecasts;
	}

	/**
	 * Parses an element of an XML file
	 * 
	 * @param selectedElement
	 *            XML element
	 * @param tag
	 *            Tag to parse.
	 * @return Data of element.
	 */
	private static String parseTag(Element selectedElement, String tag) {
		NodeList nodeList = selectedElement.getElementsByTagName(tag);
		Element element = (Element) nodeList.item(0);
		return element.getAttribute("data");
	}
}
